/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年06月29日 15:41:26
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.orm.mybatis.helper.PageHelper;
import com.pactera.jep.sys.model.Staff;
import com.pactera.jep.sys.model.StaffGroup;
import com.pactera.jep.sys.model.StaffGroupMap;
import com.pactera.jep.sys.service.StaffGroupMapService;
import com.pactera.jep.sys.service.StaffGroupService;
import com.pactera.jep.sys.service.StaffService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.util.RequestUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.pactera.jep.commons.CollectionUtils;

@Api(value = "用户组关联")
@RestController
@RequestMapping("/staffGroupMap")
public class StaffGroupMapRestController extends BaseRestController<StaffGroupMap> {
	private static Logger logger = LoggerFactory.getLogger(StaffGroupMapRestController	.class);

	@Autowired
	private StaffGroupMapService	staffGroupMapService;
	
	@Autowired
	private StaffGroupService		staffGroupService;
	
	@Autowired
	private StaffService			staffService;

	/**
	 * 为列表页面做数据准备
	 */
	@Override
	protected void prepareQuery(String token, Page<StaffGroupMap> page, List<Filter> filters, List<Order> orders,HttpServletRequest request) {
		staffGroupMapService.query(page, filters, orders);
	}

	/**
	 * 保存，用户设定的用户组
	 * @param staffCode
	 * @param jsonData
	 * @return
	 */
	@ApiOperation(value = "保存，用户设定的用户组")
	@PostMapping(value = "/saveStaffGroup")
	public Map<String, Object> saveStaffGroup(@RequestBody JSONObject jsonData) {
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try 
		{
			String staffCode = jsonData.getString("staffCode");
			String jsonStaffList = jsonData.getString("jsonStaffList");
			/*添加新设定的用户*/
			List<StaffGroup> list = JSON.parseArray(jsonStaffList, StaffGroup.class);
			staffGroupMapService.save(list, staffCode);
			
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e) 
		{
			e.printStackTrace();
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}

	/**
	 * 保存，用户组设定的用户
	 * @param jsonData
	 * @return
	 */
	@ApiOperation(value = "保存，用户组设定的用户")
	@PostMapping(value = "/saveStaffMap")
	public Map<String, Object> saveStaffMap(@RequestBody JSONObject jsonData){
		Map<String, Object> tips = new HashMap<String, Object>();
		
		try
		{
			String staffGroupCode = jsonData.getString("staffCode");
			String jsonStaffList = jsonData.getString("jsonStaffList");

			/*删除已有的关联*/
			List<Filter> filters = new ArrayList<Filter>();
			filters.add(new Filter("staffGroupCode", staffGroupCode));
			staffGroupMapService.deleteByFilter(filters);

			/*添加关联*/
			List<Staff> list = JSON.parseArray(jsonStaffList, Staff.class);
			staffGroupMapService.saveStaffMap(list, staffGroupCode);

			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch (ServiceException e)
		{
			logger.info(e.getMessage());
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		
		return tips;
	}
	
	/**
	 * 根据用户组编码获取用户列表信息
	 * @param page
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "根据用户组编码获取用户列表信息")
	@GetMapping(value = "/queryStaff")
	public Map<String, Object> queryStaff(Page<Staff> page, HttpServletRequest request){
		
		Map<String, Object> result = new HashMap<String, Object>();
		String staffGroupCode = null;
		List<Filter> filters = RequestUtils.buildFilters(request);
		if(CollectionUtils.isNotEmpty(filters))
		{
			staffGroupCode = (String) filters.get(0).getValue();
		}
		
		PageHelper.start(page);
		List<Staff> list = staffGroupMapService.queryStaff(staffGroupCode);
		PageHelper.end(page, list);
		
		result.put(RESULT_TOTAL, page.getTotal());
		result.put(RESULT_ROWS, page.getResult());
		
		return result;
	}
	@ApiOperation(value = "根据用户编码查询用户组")
	@GetMapping(value = "/selectStaffGroupByStaffCode/{staffCode}")
	public List<StaffGroupMap> selectStaffGroupByStaffCode(@PathVariable String staffCode){
		/*组装用户关联的用户组*/
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(new Filter("staffCode", staffCode));
		List<StaffGroupMap> list = staffGroupMapService.select(filters);
		return list;

	}

}